<template>
	<view class="my" >
		<view class="hasLogin" >
			<header-bar :user_data="user_data"></header-bar>
			<!-- <vip-card></vip-card> -->
			<user-set :isauth="isauth"></user-set>
		</view>
		
		<!-- 绑定手机号码 -->
		<view class="dialog-f" @click.stop="test" v-if="DialogData.phoneShow">
			<view class="needLogin" >
				<view class="user_name">
					<view class="user_name">
						<open-data type="userNickName"></open-data>
						<view class="avatar">
							<open-data  type="userAvatarUrl"></open-data>
						</view>
						<!-- <open-data type="userGender"></open-data> -->
					</view>
				</view>
				<view class="info_txt">
					此应用需要绑定您的手机号!
				</view>
				<view class="btn">
					<button type="warn" @click.stop="change">绑定手机</button>
					<!-- <button type="primary" @click="wxLogin">test</button> -->
				</view>
			</view>
		</view>
		<!-- 未登录 -->
		<view class="dialog-f" @click.stop.prevent="test" v-if="DialogData.infoShow">
			<view class="needLogin" >
				<view class="user_name">
					<view class="user_name">
						<open-data type="userNickName"></open-data>
						<view class="avatar">
							<open-data  type="userAvatarUrl"></open-data>
						</view>
						<!-- <open-data type="userGender"></open-data> -->
					</view>
				</view>
				<view class="info_txt">
					您还未登录,请使用微信登录!
				</view>
				<view class="btn">
					<button type="primary" open-type="getUserInfo" @getuserinfo="getUserInfo">微信登录</button>
					<!-- <button type="primary" @click="wxLogin">test</button> -->
				</view>
			</view>
		</view>
		
	</view>
</template>

<script>
	import HeaderBar from "@/components/public/my/HeaderBar.vue"
	import VipCard from "@/components/public/my/VipCard.vue"
	import UserSet from "@/components/public/my/UserSet.vue"
	import { post } from "@/post.js"
	export default {
		data() {
			return {
				userInfo: {},
				isauth: false,
				DialogData: {
					infoShow: true,
					phoneShow: false
				},
				openid: "",
				session_key: "",
				user_data: {
					
				},
				ckcode: "",
				belongToken: "",
				bzj: "",
			}
		},
		computed: {
			
			token() {
				return this.$store.state.token
			},
			sid() {
				return this.$store.state.sid
			},
			isLogin() {
				let res = this.token ? true: false
			},
			hasData() {
				return this.token && JSON.stringify(this.user_data) != "{}"
			}
			
		},
		watch: {
			token(newVal, oldVal) {
				if(newVal) {
					this.DialogData.infoShow = false
					this.DialogData.phoneShow = false
				} else {
					this.DialogData.infoShow = true
					this.DialogData.phoneShow = true
				}
			},
			
			
			
		},
		onLoad(e) {
			this.getUser()
			// console.log(this.token);
			if(e.scene && e.scene != this.token) {
				this.belongToken = e.scene
			}
		},
		onShow() {
			this.getopenid()
			// this.getUser()
			this.isAuthorization()
			this.isChuangke()
		},
		components: {
			HeaderBar,
			VipCard,
			UserSet,
			
		},
		methods: {
			// 获取openid
			getopenid() {
				let self = this
				uni.login({
					success( res ) {
						// console.log(res);
						if( res.code ) {
							uni.request({
								url: `${self.$Request_Url}/base/openid`,
								method: "POST",
								data: {
									code:res.code
								},
								success( info ) {
									  // console.log(info)
									let obj = info.data.result
									self.$store.commit("getSid", obj)
									// self.getToken(obj.openid)
								}
							})
						} else {
							console.log("登陆失败" + res.errMsg);
						}
					}
				})
			},
			//  查看是否是创客
			async isChuangke() {
				// console.log(this.token);
				let res = await this.$Request("POST", "/order/kanerweima",{ token: this.token })
				let code = res.data.code 
				 // console.log(res);
				if(code == 200) {
					this.ckcode = res.data.data.erweima
					this.$store.commit("getCkCode", this.ckcode)
					
				} else {
					this.ckcode = ""
					this.$store.commit("getCkCode", this.ckcode)
				}
			},

			// 是否认证
			async isAuthorization() {
				let res = await this.$Request("POST","/Index/isauthentication", { token: this.token })
				//console.log(res);
				if( res.data.code == 201) {
					this.isShow = false
				} else {
					this.isauth = true
				}
			},
			
			// 后台获取用户信息
			getUser() {
				let self = this
				//console.log(this.token);
				if(this.token) {
					this.DialogData.infoShow = false
					this.DialogData.phoneShow = false
					uni.request({
						url: `${self.$Request_Url}/Index/user`,
						method:"POST",
						data: {
							token: self.token
						},
						success( res ) {
							// console.log(res);
							self.user_data = res.data.data
							if(!res.data.data) {
								uni.removeStorageSync("miniToken")
								self.$store.commit("getUserInfo", "")
								self.$store.commit("getSid", "")
							}
						}
					})
				} else {
					this.DialogData.infoShow = true
					this.DialogData.phoneShow = true
				}
			},
			// 按钮获取用户信息
			getUserInfo( e ) {
				//console.log(e.detail.userInfo);
				this.userInfo = e.detail.userInfo
				if(this.belongToken) {
					this.userInfo.fid = this.belongToken
				}
				// console.log(this.userInfo);
				if(this.userInfo) {
					this.DialogData.infoShow = false
					this.$store.commit("getUserInfo", this.userInfo)
					//console.log(this.userInfo);
				}
				if(!this.user_data.phone) {
					this.DialogData.phoneShow = true
				}
			},
			// 是否登录
			hasLogin() {
				let self = this
				if( this.token) {
					// 获取用户信息
					uni.request({
						url: "",
						data: {
							token: self.token
						},
						success( res ) {
							self.userInfo = res.data.data
						}
					})
				} 
			},
			// test
			test() {
				//console.log("dialog");
				return false
			},
			test2() {
				//console.log("index");
			},
			change() {
				uni.navigateTo({
					url:"../phone/phone",
					fail(errMsg) {
						console.log(errMsg);
					}
				})
			},
		},
	}
	
</script>

<style lang="scss" scoped>
	.my {
		width: 100%;
		box-sizing: border-box;
		// position: relative;
		// left: 0;
		// top: 0;
		.dialog-f {
			position: fixed;
			left: 0;
			top: 0;
			width: 100%;
			height: 100%;
			background: rgba(255,255,255,0.6);
			padding-top: 30%;
			z-index: 100;
		}
		.needLogin {
			width: 70%;
			text-align: center;
			padding: 30rpx 0;
			background-color: #fff;
			margin: 0 auto;
			border-radius: 30rpx;
			overflow: hidden;
			.info_txt {
				color: #999;
			}
			.btn {
				width: 300rpx;
				margin: 20rpx auto;
			}
			.user_name {
				background-color: #fff;
				.avatar {
					width: 150rpx;
					height: 150rpx;
					border: 1px solid #f2f2f2;
					border-radius: 50%;
					margin: 20rpx auto;
					overflow: hidden;
					
				}
			}
		}
	}
</style>
